package com.definesys.smecrailtransit.adapter.LineStationAdapter;

import android.animation.ValueAnimator;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.api.SmecRailApi;
import com.definesys.smecrailtransit.base.SmecBaseRecyclerViewHolder;

import com.definesys.smecrailtransit.bean.StationBean;
import com.definesys.smecrailtransit.databinding.LayoutMonthFormItemBinding;
import com.definesys.smecrailtransit.databinding.LayoutMonthFormOmitItemBinding;
import com.definesys.smecrailtransit.databinding.LayoutPopNullDataBinding;
import com.definesys.smecrailtransit.databinding.LayoutRealStatusItemBinding;
import com.definesys.smecrailtransit.module.MonthForm.MonthFormFragment;
import com.definesys.smecrailtransit.module.TotalTime.TotalTimeFragment;
import com.definesys.smecrailtransit.module.SiteDetails.SiteDetailsPresenter;
import com.definesys.smecrailtransit.module.SiteDetails.dialog.SiteReaErrorDialog;
import com.hwangjr.rxbus.Bus;


import java.util.ArrayList;
import java.util.HashMap;


/**
 * Created by cjy on 2017/7/23.
 */

public class LineStationAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder> {
    private static int NORMAL = 1;
    private static int DOT = -1;
    private static int NULL = 0;

    private ArrayList<StationBean> list;
    private DisplayMetrics dm;
    private int maxNumber;
    protected Bus mRxbus;
    private String type;
    private Context mContext;
    private SiteDetailsPresenter siteDetailsPresenter;
    private SiteReaErrorDialog siteReaErrorDialog;
    public boolean showAnimation = true;

    private HashMap<String,Integer> hashMap=new HashMap<>();
    public LineStationAdapter(DisplayMetrics dm, Bus mRxbus, String type, Context context){
        list=new ArrayList<>();
        this.mContext=context;
        this.dm = dm;
        this.mRxbus = mRxbus;
        this.type = type;
        if(type !=null && type.equals(SmecRailApi.MonthCode.REALSTATUS)){
            siteDetailsPresenter=new SiteDetailsPresenter(mContext);
        }
    }

    public void setList(ArrayList<StationBean> list) {
        if(list != null && list.size() != 0){
            this.list = list;
            this.maxNumber = this.list.get(0).getValue();
            for(int i=1 ;i < this.list.size(); i++){
                if(maxNumber < this.list.get(i).getValue() && this.list.get(i).getSeq() != -1){
                    maxNumber = this.list.get(i).getValue();
                }
            }
        }else{
            this.list = new ArrayList<>();
        }

        if(type.equals(SmecRailApi.MonthCode.TOTALTIME)){
            if(maxNumber > 550){
                maxNumber = 500;
            }
        }else{
            if(maxNumber > 20){
                maxNumber = 20;
            }
        }

        notifyDataSetChanged();
    }

    public ArrayList<StationBean> getList(){
        return this.list;
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == NULL){
            LayoutPopNullDataBinding layoutPopNullDataBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext())
                    , R.layout.layout_pop_null_data,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                    SmecBaseRecyclerViewHolder(layoutPopNullDataBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutPopNullDataBinding);
            return smecBaseRecyclerViewHolder;
        } else if(viewType == NORMAL){

            if( type.equals(SmecRailApi.MonthCode.REALSTATUS)){
                LayoutRealStatusItemBinding layoutRealStatusItemBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext())
                        , R.layout.layout_real_status_item,parent,false);
                SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                        SmecBaseRecyclerViewHolder(layoutRealStatusItemBinding.getRoot());
                smecBaseRecyclerViewHolder.setViewType(viewType);
                smecBaseRecyclerViewHolder.setBinding(layoutRealStatusItemBinding);
                return smecBaseRecyclerViewHolder;
            }else{
                LayoutMonthFormItemBinding layoutMonthFormItemBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext())
                        , R.layout.layout_month_form_item,parent,false);
                SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                        SmecBaseRecyclerViewHolder(layoutMonthFormItemBinding.getRoot());
                smecBaseRecyclerViewHolder.setViewType(viewType);
                smecBaseRecyclerViewHolder.setBinding(layoutMonthFormItemBinding);
                return smecBaseRecyclerViewHolder;
            }

        }else{
            LayoutMonthFormOmitItemBinding layoutMonthFormItemBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext())
                    , R.layout.layout_month_form_omit_item,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                    SmecBaseRecyclerViewHolder(layoutMonthFormItemBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutMonthFormItemBinding);
            return smecBaseRecyclerViewHolder;
        }

    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, int position) {
        if(holder.getViewType() == NORMAL){

            if(type.equals(SmecRailApi.MonthCode.REALSTATUS)){
                LayoutRealStatusItemBinding viewDataBinding=(LayoutRealStatusItemBinding)holder.getBinding();
                viewDataBinding.site.setText(list.get(position).getName());

                viewDataBinding.number.setText(String.valueOf(list.get(position).getFaultValue()) + "/" +
                        String.valueOf(list.get(position).getValue())+"台");

                viewDataBinding.item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showDialog(list.get(position).getStationId(),list.get(position).getName());
                    }
                });

                int width = 0 ;
                if( this.list.get(position).getValue() < this.maxNumber){
                    width = this.dm.widthPixels * 1/3 * this.list.get(position).getValue() / this.maxNumber;
                    viewDataBinding.pillarFault.getLayoutParams().width = dm.widthPixels * 1/3 * list.get(position).getFaultValue() / list.get(position).getValue();
                }else{
                    width = this.dm.widthPixels* 1/3;
                    if(maxNumber != 0) {
                        viewDataBinding.pillarFault.getLayoutParams().width = dm.widthPixels * 1/3 * list.get(position).getFaultValue() / maxNumber;
                    }else{
                        viewDataBinding.pillarFault.getLayoutParams().width = 0;
                    }
                }

                if(maxNumber != 0){
                    if(this.showAnimation){
                        Animation(viewDataBinding.pillar,width);
                    }else{
                        viewDataBinding.pillar.getLayoutParams().width = width;
                    }
                }else{
                    viewDataBinding.pillar.getLayoutParams().width = 0;
                }
                viewDataBinding.invalidateAll();



            }else{
                LayoutMonthFormItemBinding viewDataBinding=(LayoutMonthFormItemBinding)holder.getBinding();
                viewDataBinding.site.setText(list.get(position).getName());

                if(type.equals(SmecRailApi.MonthCode.MONTHFORM)){
                    viewDataBinding.number.setText(String.valueOf(list.get(position).getValue())+"次");
                }
                if(type.equals(SmecRailApi.MonthCode.TOTALTIME)){
                    viewDataBinding.number.setText(String.valueOf(list.get(position).getValue())+"小时");
                }

                viewDataBinding.item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if(LineStationAdapter.this.type.equals(SmecRailApi.MonthCode.MONTHFORM)){
                            HashMap<String,Object> map = new HashMap<>();
                            map.put("value",LineStationAdapter.this.list.get(position).getValue());
                            map.put("lineId",LineStationAdapter.this.list.get(position).getLineId());
                            map.put("stationId",LineStationAdapter.this.list.get(position).getStationId());
                            map.put("stationName",LineStationAdapter.this.list.get(position).getName());
                            map.put("date", LineStationAdapter.this.list.get(position).getDate());
                            mRxbus.post(MonthFormFragment.GET_DETAIL, map);
                        }
                        if(LineStationAdapter.this.type.equals(SmecRailApi.MonthCode.TOTALTIME)){
                            HashMap<String,Object> map = new HashMap<>();
                            map.put("stationId",LineStationAdapter.this.list.get(position).getStationId());
                            map.put("stationName",LineStationAdapter.this.list.get(position).getName());
                            mRxbus.post(TotalTimeFragment.SHOW_STATION_TIME, map);
                        }

                    }
                });

                int width = 0 ;
                if( this.list.get(position).getValue() < this.maxNumber){
                    width = this.dm.widthPixels*2/5*this.list.get(position).getValue()/this.maxNumber;
                }else{
                    width = this.dm.widthPixels*2/5;
                }

                if(maxNumber != 0){
                    if(this.showAnimation){
                        Animation(viewDataBinding.pillar,width);
                    }else{
                        viewDataBinding.pillar.getLayoutParams().width = width;
                    }
                }else{
                    viewDataBinding.pillar.getLayoutParams().width = 0;
                }
                viewDataBinding.invalidateAll();
            }

        }
    }

    @Override
    public int getItemCount() {
        if(list.size() == 0 ){
            return 1;
        }else {
            return this.list.size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(list.size() == 0){
            return NULL;
        }else {
            if(this.list.get(position).getSeq() != -1){
                return NORMAL;
            }else {
                return DOT;
            }
        }
    }

    private void showDialog(int id,String name){
        hashMap.put("stationId",id);
        siteDetailsPresenter.getRealTimeStatus(hashMap);
        if(siteReaErrorDialog == null){
            siteReaErrorDialog=new SiteReaErrorDialog(mContext,R.style.project_back,false,siteDetailsPresenter);
        }
        siteReaErrorDialog.setStationName(name);
        siteReaErrorDialog.show();
    }

    private void Animation(View v,int width){
        //属性动画对象
        ValueAnimator va ;
        //显示view，高度从0变到height值
        va = ValueAnimator.ofInt(0,width);
        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                //获取当前的height值
                int w =(Integer)valueAnimator.getAnimatedValue();
                //动态更新view的高度
                v.getLayoutParams().width = w;
                v.requestLayout();
            }
        });
        va.setDuration(800);
        //开始动画
        va.start();
    }
}
